package battlebarnserver.util;

import battlebarnapplet.entities.LocationEntity;
import battlebarnapplet.entities.VelocityEntity;
import battlebarnserver.BattleBarn;
import battlebarnserver.entities.BaseEntity;
import battlebarnserver.entities.MovementEntity;
import battlebarnserver.entities.PlayerEntity;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

/**
 * 
 * @author johncreecy
 */
public class Conversions {

	public static synchronized int tileToPixel(int tile) {
		return (tile * Constants.PIXELS_PER_TILE);
	}

	public static synchronized int pixelToTile(int pixel) {
		return (pixel / Constants.PIXELS_PER_TILE);
	}

	public static synchronized String padCoordinate(String coordinate) {
		String paddedCoordinate = "";
		switch (coordinate.length()) {
		case 0:
			paddedCoordinate = "000";
			break;
		case 1:
			paddedCoordinate = "00" + coordinate;
			break;
		case 2:
			paddedCoordinate = "0" + coordinate;
			break;
		default:
			paddedCoordinate = coordinate;
			break;
		}
		return paddedCoordinate;
	}
	
	
	public static synchronized String padHealth(int health) {
		String paddedHealth = "";
		String healthString = Integer.toString(health);
		switch (healthString.length()) {
		case 0:
			paddedHealth = "000";
			break;
		case 1:
			paddedHealth = "00" + health;
			break;
		case 2:
			paddedHealth= "0" + health;
			break;
		default:
			paddedHealth = healthString;
			break;
		}
		return paddedHealth;
	}

	public static void calculateMovement(int movementDirection) {
		//
	}

	public static synchronized int getBoundsX(int tileX) {
		if (tileX >= Constants.TILE_MAX_X) {
			return Constants.TILE_MAX_X;
		} else if (tileX <= Constants.TILE_MIN_X) {
			return Constants.TILE_MIN_X;
		} else {
			return tileX;
		}
	}

	public static synchronized int getBoundsY(int tileY) {
		if (tileY >= Constants.TILE_MAX_Y) {
			return Constants.TILE_MAX_Y;
		} else if (tileY <= Constants.TILE_MIN_Y) {
			return Constants.TILE_MIN_Y;
		} else {
			return tileY;
		}
	}

	public static synchronized boolean testPixelBoundsY(int tileY) {
		if (tileY > (Constants.PIXEL_MAX_Y)) {
			return false;
		} else if (tileY < (Constants.PIXEL_MIN_Y)) {
			return false;
		} else {
			return true;
		}
	}

	public static synchronized boolean testPixelBoundsX(int tileX) {
		if (tileX > (Constants.PIXEL_MAX_X)) {
			return false;
		} else if (tileX < (Constants.PIXEL_MIN_Y)) {
			return false;
		} else {
			return true;
		}
	}
	
	public static synchronized boolean testBoundsY(int tileY) {
		if (tileY > (Constants.TILE_MAX_Y + 1)) {
			return false;
		} else if (tileY < (Constants.TILE_MIN_Y - 1)) {
			return false;
		} else {
			return true;
		}
	}

	public static synchronized boolean testBoundsX(int tileX) {
		if (tileX > (Constants.TILE_MAX_X + 1)) {
			return false;
		} else if (tileX < (Constants.TILE_MIN_X - 1)) {
			return false;
		} else {
			return true;
		}
	}

	public static synchronized boolean collisionTest(int tileX, int tileY) {
		Iterator it;
		it = BattleBarn.actionStack.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry mapEntry = (Map.Entry) it.next();
			BaseEntity baseEntity = (BaseEntity) mapEntry.getValue();
			if (baseEntity.getTileX() == tileX && baseEntity.getTileY() == tileY) {
				return false;
			}
		}
		for (int i = 0; i < BattleBarn.staticGameData.size(); i++) {
			BaseEntity baseEntity = BattleBarn.staticGameData.get(i);
			if (baseEntity.getTileX() == tileX && baseEntity.getTileY() == tileY) {
				return false;
			}
		}
		return true;
	}

	public static synchronized int getVelocityX(int facing) {
		int retVal;
		switch (facing) {
		case 2:
			retVal = 1;
			break;
		case 4:
			retVal = -1;
			break;
		default:
			retVal = 0;
			break;
		}
		return retVal;
	}

	public static synchronized int getVelocityY(int facing) {
		int retVal;
		switch (facing) {
		case 1:
			retVal = -1;
			break;
		case 3:
			retVal = 1;
			break;
		default:
			retVal = 0;
			break;
		}
		return retVal;
	}

	public static synchronized String padVelocity(int velocity) {
		if (velocity == 1) {
			return "01";
		} else {
			return Integer.toString(velocity);
		}
	}

	public static synchronized int getDirectionId(int velocityX, int velocityY) {
		int direction; // 0 none, 1 up, 2 right, 3 down, 4 left
		if (velocityX == 1) {
			direction = 2;
		} else if (velocityX == -1) {
			direction = 4;
		} else if (velocityY == 1) {
			direction = 1;
		} else if (velocityY == -1) {
			direction = 3;
		} else {
			direction = 2; // failsafe
		}
		return direction;
	}

	public static synchronized VelocityEntity getVelocityPair(int directionId) {
		// 0 none, 1 up, 2 right, 3 down, 4 left
		// x0y0, x0y1, x1y0, x0y-1, x-1y0
		VelocityEntity velocityEntity;
		if (directionId == 1) {
			velocityEntity = new VelocityEntity(0, 1);
		} else if (directionId == 2){
			velocityEntity = new VelocityEntity(1, 0);
		} else if (directionId == 3){
			velocityEntity = new VelocityEntity(0, -1);
		} else if (directionId == 4){
			velocityEntity = new VelocityEntity(-1, 0);
		} else {
			velocityEntity = new VelocityEntity(0, 0);
		}
		
		return velocityEntity;
	}
	
	public static MovementEntity generateRandomMovement(int tileX, int tileY, int facing) {
		Random generator = new Random();
		int randomAction = generator.nextInt(100);
		int newTileX = tileX;
		int newTileY = tileY;
		int newFacing = facing;
		if(randomAction > 96) {
			if(randomAction == 97)  {
			newTileY = tileY + 1;
			facing = 1;
			} else if (randomAction == 98) {
				newTileX = tileX + 1;
				facing = 2;
			}	else if (randomAction == 99) {
				newTileY = tileY - 1;
				facing = 3;
			}	else if (randomAction == 100) {
				newTileX = tileX - 1;
				facing = 4;
			}	
		}
		MovementEntity movementEntity = new MovementEntity(Conversions.getBoundsX(newTileX), Conversions.getBoundsY(newTileY), newFacing);
		return movementEntity;
	}
	
	public static synchronized int boolToInt(boolean inputValue) {
		if(inputValue == true) {
			return 1;
		} else {
			return 0;
		}
	}
	
}
